// store/user.js
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: uni.getStorageSync('token') || '',
    user: uni.getStorageSync('user') ? JSON.parse(uni.getStorageSync('user')) : null,
    role: uni.getStorageSync('role') || ''
  }),
  getters: {
    isLogin: (state) => !!state.token,
    userInfo: (state) => state.user,
    userRole: (state) => state.role,
    isSupervisor: (state) => state.role === 'supervisor'
  },
  actions: {
    setUser(payload) {
      this.token = payload.token
      this.user = payload.user
      this.role = payload.role
      uni.setStorageSync('token', payload.token)
      uni.setStorageSync('user', JSON.stringify(payload.user))
      uni.setStorageSync('role', payload.role)
    },
    logout() {
      this.token = ''
      this.user = null
      this.role = ''
      uni.removeStorageSync('token')
      uni.removeStorageSync('user')
      uni.removeStorageSync('role')
    }
  }
})
